define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'csmitsm_xcore'], function ($, undefined, Backend, Table, Form, xcore) {

    var Controller = {
        _queryString: '',
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'csmitsm/xcflowviewpage/index' + Controller._queryString,
                    add_url: 'csmitsm/xcflowviewpage/add' + Controller._queryString,
                    // edit_url: 'csmitsm/xcflowviewpage/edit',
                    del_url: 'csmitsm/xcflowviewpage/del',
                    multi_url: 'csmitsm/xcflowviewpage/multi',
                    import_url: 'csmitsm/xcflowviewpage/import',
                    table: 'csmitsm_xcflowviewpage',
                }
            });

            $(".btn-dbview").click(function(){
                window.open(Fast.api.fixurl("csmitsm/xcflowdbview/index"));
            });

            Fast.config.openArea = ['98%', '98%'];
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id')
                        },
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE'
                        },
                        {
                            field: 'menuname',
                            title: __('Menuname'),
                            operate: 'LIKE'
                        },
                        {
                            field: 'admin_id',
                            title: __('Admin_id')
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'weigh',
                            title: __('Weigh'),
                            operate: false
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {
                                "normal": __('Normal'),
                                "hidden": __('Hidden')
                            },
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [{
                                name: 'click',
                                title: function (row) {
                                    return row.name;
                                },
                                classname: 'btn btn-xs btn-warning btn-click',
                                icon: 'fa fa-folder-o',
                                click: function (data,row) {
                                    xcore.openWindow(Fast.api.fixurl('csmitsm/xcflowviewpage/edit/ids/'+row.id)+"?dialog=1");
                                }
                            }]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.api.rebuildAddUrl(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        publishmenu: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            queryString: function () {
                return location.search.replace("dialog=1", "").split('&').filter(function (item) {
                    return !!item;
                }).join("&");
            },
            rebuildAddUrl: function (table) {
                var $tabs = $('.nav-tabs[data-field]');
                if ($tabs.length > 0) {
                    var field = $tabs.data("field");
                    var options = table.bootstrapTable('getOptions');
                    table.on("pre-body.bs.table", function () {
                        var activeTab = $('.active a', $tabs);
                        var value = activeTab.data("value");
                        var reg = new RegExp(field + "\=(.*?)");
                        var queryString = location.search
                            .replace("dialog=1", "")
                            .replace(reg, "")
                            .split('&')
                            .filter(function (item) {
                                return !!item;
                            }).join("&");
                        if (queryString.indexOf("?") == 0) {
                            queryString = queryString + "&" + field + "=" + value
                        } else {
                            queryString = queryString + "?" + field + "=" + value
                        }
                        options.extend.add_url = 'csmitsm/xcflowviewpage/add' + queryString
                    })
                }
            }
        }
    };
    Controller._queryString = Controller.api.queryString();
    return Controller;
});